package com.sc.core.util;

import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class RouteScanner {
    private static final Logger log = LoggerFactory.getLogger(RouteScanner.class);
    private final RequestMappingHandlerMapping requestMappingHandlerMapping;

    @Getter
    private final List<String> routes = new ArrayList<>();

    public RouteScanner(RequestMappingHandlerMapping requestMappingHandlerMapping) {
        this.requestMappingHandlerMapping = requestMappingHandlerMapping;

        scanRoutes();
    }

    private void scanRoutes() {
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
        for (RequestMappingInfo info : handlerMethods.keySet()) {
            // 获取所有 URL 模式
            if (info.getPathPatternsCondition() != null) {
                List<String> paths = info.getPathPatternsCondition().getPatternValues()
                        .stream()
                        .filter(path -> path.startsWith("/admin"))
                        .toList();
                routes.addAll(paths);
            }
        }
    }

}
